﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using Kinect;
using System.Windows.Shapes;
using System.Windows.Media;

namespace KinectApplication
{
    public static class Debugging
    {
        public static Image image;
        public static Image hand;
        public static Image hand2;
        public static Canvas canvas;

        public static void ShowDepth(byte[] source, int width, int height)
        {
            byte[] depth = source.ConvertDepthFrameDataToBitmapData(width, height);
            image.Source = depth.ToBitmapSource(width, height);
        }

        public static void ShowDepth(short[] source, int width, int height)
        {
            byte[] depth = source.ConvertDepthFrameDataToBitmapData(width, height);
            image.Source = depth.ToBitmapSource(width, height);
        }

        public static void ShowDepthWithPlayer(byte[] source, int width, int height)
        {
            byte[] depth = source.ConvertDepthFrameDataWithSkeletonToBitmapData(width, height);
            image.Source = depth.ToBitmapSource(width, height);
        }

        public static void ShowBlob(Blob blob, float x, float y)
        {
            //canvas.Children.Clear();

            
            Rectangle r = new Rectangle();
            r.Width = blob.Width;
            r.Height = blob.Height;
            r.Stroke = new SolidColorBrush(Colors.Green);
            canvas.Children.Add(r);

            Canvas.SetLeft(r, blob.CenterOfGravity.X - (blob.Width / 2) );
            Canvas.SetTop(r, blob.CenterOfGravity.Y - (blob.Height / 2) );
            
        }

        public static void ShowRectangle(int x, int y, int width, int height)
        {
            //canvas.Children.Clear();


            Rectangle r = new Rectangle();
            r.Width = width;
            r.Height = height;
            r.Stroke = new SolidColorBrush(Colors.Green);
            canvas.Children.Add(r);

            Canvas.SetLeft(r, x);
            Canvas.SetTop(r, y);

        }

        public static void ShowHand(int Width, int Height, int X, int Y)
        {
          
            
            Rectangle r = new Rectangle();
            r.Width = Width;
            r.Height = Height;
            r.Stroke = new SolidColorBrush(Colors.Red);
            r.Fill = new SolidColorBrush(Colors.Red);
            canvas.Children.Add(r);

            Canvas.SetLeft(r, X - (Width / 2));
            Canvas.SetTop(r, Y - (Height / 2));
            
        }

        public static void Clear()
        {
            canvas.Children.Clear();
        }

        public static void ShowBlobs(List<Blob> blobs)
        {
            canvas.Children.Clear();

            foreach (var item in blobs)
            {
                Rectangle r = new Rectangle();
                r.Width = item.Width;
                r.Height = item.Height;
                r.Stroke = new SolidColorBrush(Colors.Red);
                canvas.Children.Add(r);

                Canvas.SetLeft(r, item.CenterOfGravity.X - (item.Width / 2));
                Canvas.SetTop(r, item.CenterOfGravity.Y - (item.Height / 2));
            }
        }

        public static void ShowHand(short[] source, int width, int height)
        {
            //scaled to 24 x 32
            hand.Source = source.ResizeBilinear(width, height, 24, 32).ConvertDepthFrameDataToBitmapData(24, 32).ToBitmapSource(24, 32);

            //original
            hand2.Source = source.ConvertDepthFrameDataToBitmapData(width, height).ToBitmapSource(width, height);
        }

        public static void ShowXY(int x, int y)
        {
            canvas.Children.Clear();


            Rectangle r = new Rectangle();
            r.Width = 10;
            r.Height = 10;
            r.Stroke = new SolidColorBrush(Colors.Red);
            canvas.Children.Add(r);

            Canvas.SetLeft(r, x - 5);
            Canvas.SetTop(r, y - 5);


        }

        public static void ShowParserHand(short[] source, int width, int height, short[] rezSource, int rezWidth, int rezHeight, HandGestures gesture)
        {
            // Parsers scaled input
            hand.Source = rezSource.ConvertDepthFrameDataToBitmapData(rezWidth, rezHeight).ToBitmapSource(rezWidth, rezHeight);

            // Orginal input
            hand2.Source = source.ConvertDepthFrameDataToBitmapData(width, height).ToBitmapSource(width, height);

            // Gesture info - Ugly corner block
            canvas.Children.Clear();

            Rectangle r = new Rectangle();
            r.Width = 15;
            r.Height = 15;

            if (gesture == HandGestures.Neutral)
            {
                r.Stroke = new SolidColorBrush(Colors.Blue);
                r.Fill = new SolidColorBrush(Colors.Blue);
            }
            else if (gesture == HandGestures.A)
            {
                r.Stroke = new SolidColorBrush(Colors.Yellow);
                r.Fill = new SolidColorBrush(Colors.Yellow);
            }
            else if (gesture == HandGestures.B)
            {
                r.Stroke = new SolidColorBrush(Colors.Red);
                r.Fill = new SolidColorBrush(Colors.Red);
            }
            else
            {
                r.Stroke = new SolidColorBrush(Colors.White);
                r.Fill = new SolidColorBrush(Colors.White);
            }

            canvas.Children.Add(r);

            Canvas.SetLeft(r, 0);
            Canvas.SetTop(r, 0);
        }
    }
}
